﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace utils
{
    public class Tool
    {
        public static class HexConverter
        {
            // 将字节数组转换为指定格式的十六进制字符串
            public static string ByteArrayToFormattedHexString(byte[] bytes)
            {
                StringBuilder sb = new StringBuilder();
                foreach (byte b in bytes)
                {
                    sb.Append(b.ToString("X2"));
                }
                return sb.ToString();
            }
        }

        public static int GenerateSerFile(List<string> macAddresses, string filePath,long addr,string chipName)
        {
            // 检查文件路径是否已存在
            if (File.Exists(filePath))
            {
                return 500;
            }

            using (StreamWriter writer = new StreamWriter(filePath))
            {
                // 写入注释行
                writer.WriteLine(";============Acroview Co.,Ltd.======================");
                writer.WriteLine(";This file is created by SN generator automatically");
                writer.WriteLine(";Please don't modify it manually");
                writer.WriteLine(";===================================================");
                writer.WriteLine("\n");
                writer.WriteLine("[Chip Info]");
                writer.WriteLine("ChipName:" + chipName );
                writer.WriteLine("ByteMode:LE");
                writer.WriteLine("DataMode:Data");
                writer.WriteLine("\n");
                writer.WriteLine("\n");
                writer.WriteLine(";<label><address1><size1><data1>\n");
                writer.WriteLine("[SN Info]");

                // 写入每个 MAC 地址
                int idx = 1;
                //int baseAddress = 0x100; // 起始地址
                int size = 6; // 每个 MAC 地址的大小

                foreach (var mac in macAddresses)
                {
                    string idxStr = idx.ToString("D8"); // 格式化 idx
                    string addressStr = addr.ToString("X");
                    string sizeStr = size.ToString("X");
                    string formattedMac = FormatMacAddress(mac);

                    writer.WriteLine($"<L:{idxStr}><{addressStr}><{sizeStr}><{formattedMac}>");

                    idx++;
                    //baseAddress += size; // 更新地址
                }

                writer.WriteLine("END");
            }
            return 200;
        }

        public static string FormatMacAddress(string mac)
        {
            var formattedMac = string.Join(" ", Enumerable.Range(0, mac.Length / 2)
                .Select(i => mac.Substring(i * 2, 2)));
            return formattedMac;
        }

        public static string ToUnixTimeSeconds(DateTime dateTime)
        {
            DateTimeOffset dto = new DateTimeOffset(dateTime, TimeSpan.Zero);
            long unixTimeSeconds = (long)(dto - new DateTimeOffset(new DateTime(1970, 1, 1), TimeSpan.Zero)).TotalSeconds;
            return unixTimeSeconds.ToString();
        }
    }
}
